let router = require('../router/Router');

let config = require('../config/config');
let P = require("bluebird");

let Token = require('../token');
let http = require('http');
let cnst = require('../const');
let fileUtil = require('../util/fileUtil');
let ipUtil = require('../util/ipUtil');
let wxsign = require('../util/wxsign');
let wxUtil = require('../util/wxUtil');
let userService = require('../service/userService');
let redis=require('../dao/redis');
let wxService = require('../service/wxService')
let redisConst = require('../const/redisConst')
let tokenService = require('../service/TokenService')
let userDao = require('../dao/userDao')
//let Token = require('../token');
//let redis=new predis({port:6379,host:'101.37.164.134'});

const { BusiError, SysError } = require('../exception/exception');

router.get('/xianliao/getimg', function (req, res) {
    //let imgurl = new Buffer(req.query.imgurl,'base64').toString();
    //http://weixin.g9527.com:3013/active/getimg?imgurl=http%3A%2F%2Fwx.qlogo.cn%2Fmmopen%2Fk0Ue4mIpaVicAXoZ3uiadCxaeJia2P1n1Puqz8cSmrYRjGzYplFpWyBaph981jQWUu2tD7hZwH29jDclN7sjAC3GQ%2F0&type=a.jpg
    let imgurl = decodeURIComponent(req.query.imgurl);
    console.logw('get img', imgurl);
    http.get(imgurl, function (res0) {
        let type = res0.headers['content-type'];
        res.setHeader('content-type', type);
        res0.pipe(res);
    }).on('error', function (err) {
        console.logw('http getImage error', err);
        res.header("Cache-Control", "public, max-age=0");
        res.send({ code: 501, msg: 'get remote img error' });
    }).on('end', function (err) {
        console.logw('redirect img end');
        res.end();
    }).setTimeout(5000, function () {
        console.logw('get img timeout');
        res.end();
    });
});

function httpRequest(url, cb) {
    http.get(url, function (req, res) {
        let html = '';
        req.on('data', function (data) {
            html += data;
        });
        req.on('end', function () {

            cb(0, html);
        });
        req.on('error', function () {
            cb(0, '');
        });
    });
}

router.httpGet = P.coroutine(function* (url) {
    let getAsync = P.promisify(httpRequest);
    let res = yield getAsync(url);
    return res;
});


router.getaccesstoken = P.coroutine(function* (code) {
    let getAsync = P.promisify(accesstoken);
    let res = yield getAsync(code);
    return res;
});
function accesstoken(code,cb) {
    let https = require('https');
    let querystring = require('querystring');

    let contents = querystring.stringify({
        appid: 'KSbtLzOi6v7TUdyG',
        appsecret: 'XZPBOLKvtfS8nu7M',// XZPBOLKvtfS8nu7M
        grant_type: 'authorization_code',
        code:code

    });

    let options = {
        host: 'ssgw.updrips.com',
        path: '/oauth2/accessToken',
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': contents.length
        }
    }
    let req = https.request(options, function (res) {
        res.setEncoding('utf8');
        let html = '';
        res.on('data', function (data) {
            console.log(data);
            html += data;
        });
        res.on('end', function () {

            cb(0, html);
        });
        res.on('error', function () {
            cb(0, '');
        });
    });

    req.write(contents);
    req.end;

}

router.getuserinfo = P.coroutine(function* (token) {
    let getAsync = P.promisify(userinfo);
    let res = yield getAsync(token);
    return res;
});
function userinfo(token,cb) {
    let https = require('https');
    let querystring = require('querystring');

    let contents = querystring.stringify({
        access_token:token

    });
    let options = {
        host: 'ssgw.updrips.com',
        path: '/resource/user/getUserInfo',
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': contents.length
        }
    }
    let req = https.request(options, function (res) {
        res.setEncoding('utf8');
        let html = '';
        res.on('data', function (data) {
            console.log(data);
            html += data;
        });
        res.on('end', function () {

            cb(0, html);
        });
        res.on('error', function () {
            cb(0, '');
        });
    });

    req.write(contents);
    req.end;

}

//http://xianliao.meilidashi.com:8222/api/index.html?code=89b96a81e0594fa50ecf38abb68091f2
//http://localhost:3013/api/xianliao/login?code=dcb51811727b9fc3f74ed9950a92e3b2
//dcb51811727b9fc3f74ed9950a92e3b2
//http://xianliao.meilidashi.com/xiaoliao/h5_fk_hall/game.html


router.get('/api/xianliao/login',function(req,res){
    let code = req.query.code;

    // let xxCode=req.query.xxCode;
    // let  token=req.query.token;
    let  ip= ipUtil.getIp(req,res);
    let stoken=Token.aesEncodeDef(ip);
    // let os='';
    // //let uid=-1;
    let doAsync= P.coroutine(function*() {
        // let  needAuth=true;
        // let  offlineData='';
        // let  version=yield redis.getString("version");
        // if(!version)  version='default';
        // console.log("xxCode ",xxCode)
        let accesstoken = "";
        let refreshtoken = "";
        let result = yield router.getaccesstoken(code);
        console.log(result, "return here");
        let realres = JSON.parse(result);
        console.log(realres);
        if (realres.err_code !== 0) {
            let ret = {
                code:500,
                errmsg:realres.err_msg,
            };
            res.send(ret);
            return;


        } else {
            accesstoken = realres.data.access_token;
            // await redis.set(cacheKey, accesstoken, 2 * 3600);
            refreshtoken = realres.data.refresh_token;
            let userinfo = yield router.getuserinfo(accesstoken);
            let userinfoentry = JSON.parse(userinfo);
            if (userinfoentry.err_code !== 0) {
                let ret = {
                    code:500,
                    errmsg:userinfoentry.err_msg,
                };
                res.send(ret);
                return;

            } else {
                console.log(userinfoentry.data,"xxxxxxxxxxxxxxxxxxxxxxxxxx");
                let nickname = userinfoentry.data.nickName;
                let headimgurl = userinfoentry.data.originalAvatar;
                let account = "xl_" + userinfoentry.data.openId;
                let resData= yield userService.saveXlUserInfo(account,nickname,headimgurl);
                console.log(resData.suc);
                if(resData.suc === true) {
                    console.logw('xianliao_auth uid ',resData.uid);
                    let resAccess = yield userDao.getOrCreateUserAccessAsync(resData.uid,nickname);
                    if(resAccess && resAccess.ban_level > 0) {
                        let info=nickname+' 此号'+resData.uid+'已被封';
                        console.logw(info,'ip',ip);
                        let ret = {
                            code:500,
                            errmsg:info,
                        };
                        res.send(ret);
                        return;

                    }
                    let token=Token.aesEncodeDef(''+resData.uid);//加密用户id
                    let ret = {
                        code:200,
                        errmsg:"ok",
                        account:account,
                        headimgurl:headimgurl,
                        name:nickname,
                        cardNum:0,
                    };
                    ret.uid=resData.uid;
                    ret.isAgent=resData.isAgent;
                    ret.inviteId=resData.inviteId;
                    ret.cardNum=resData.cardNum;
                    ret.phone=resData.phone;
                    ret.phoneCode=resData.phoneCode;
                    ret.codeNocheck=resData.codeNocheck;
                    ret.token=token;
                    ret.debugFlag=resData.debugFlag;
                    ret.wxNickName=resData.name;
                    ret.stoken=stoken;
                    ret.phoneNoCheck=1;
                    var  xxCode=yield   userService.makeXxCode(ret.uid);
                    ret.xxCode=xxCode;
                    // ret.version=version;
                    console.log('xianliao_auth uid--------------------- ',ret);
                    res.send(ret);
                    return;
                }else {
                    console.logw('createXlUserAsync failed . ');
                    let ret = {
                        code:500,
                        errmsg:'createXlUserAsync failed . ',
                    };
                    res.send(ret);
                    return;
                }
            }
        }
    });

    //     if(xxCode)
    //     {
    //         var   userUid=yield userService.getUidByXxCode(xxCode)
    //         console.logw("xxCode ",xxCode,userUid)
    //         if(userUid)
    //         {
    //             console.logw("从xxCode中获得用户uid 进行登录")
    //             var  userInfo=yield  userService.getUserInfoByUid(userUid);
    //             if(userInfo)
    //             {
    //                 var  ret=userInfo;
    //                 ret.password="";
    //                 ret.token=yield tokenService.updateToken(ret.uid);
    //                 ret.stoken=stoken;
    //                 ret.phoneNoCheck=1;
    //                 ret.version=version;
    //                 ret.code=200;
    //                 if(ret.phoneCode === ret.codeNocheck && ret.phoneCode)  ret.phoneNoCheck=0;
    //                 console.logw('login by  xxCode ',JSON.stringify(ret));
    //                 res.send(ret);
    //                 return;
    //             }
    //
    //         }
    //     }
    //
    //     if(token&&!code)
    //     {
    //         try {
    //             let validRes = yield tokenService.validToken(token);
    //             var uid = validRes;
    //         } catch (e) {
    //             console.logw("token valid failed:", e.message, e);
    //             uid=null;
    //             //throw new BusiError(500, "无效token");
    //         }
    //         if(uid>100)
    //         {
    //             console.logw("从token中获得用户uid 进行登录 ",uid)
    //             var  userInfo=yield  userService.getUserInfoByUid(uid);
    //             if(userInfo)
    //             {
    //                 var  ret=userInfo;
    //                 ret.password="";
    //                 ret.token=yield tokenService.updateToken(ret.uid);
    //                 ret.stoken=stoken;
    //                 ret.phoneNoCheck=1;
    //                 ret.version=version;
    //                 ret.code=200;
    //                 if(ret.phoneCode === ret.codeNocheck && ret.phoneCode)  ret.phoneNoCheck=0;
    //                 console.logw('login by  token ',JSON.stringify(ret));
    //                 res.send(ret);
    //                 return;
    //             }
    //         }
    //
    //     }
    //
    //
    //     let  accessInfo=yield wxService.getWxCodeCache(code);
    //     //判断缓存中是否存在微信信息，如果不存在则调用授权接口获取
    //     if(!accessInfo) {
    //         //没有accessinfo的 情况
    //         accessInfo = yield wxUtil.getAccessToken(code,os);
    //     }
    //     if(typeof accessInfo === 'string')
    //         accessInfo=JSON.parse(accessInfo);
    //     let time=(new Date().getTime()-accessInfo.tick)/1000;
    //     let unionid=accessInfo.unionid;
    //     let wxopenid="wx_"+accessInfo.openid;
    //     let openid = wxopenid;
    //     console.logg("unionid="+unionid)
    //     if(accessInfo.unionid) {
    //         yield  redis.setHashItem(redisConst.wxUnionMap,unionid,wxopenid);
    //         yield  redis.setHashItem(redisConst.wxUnionMap,wxopenid,unionid);
    //     }
    //     //判断access_token调用时间是否超时
    //     if(time >= accessInfo.expires_in){
    //         accessInfo = yield wxUtil.getAccessToken(code,os);
    //         if(typeof accessInfo === 'string')
    //             accessInfo=JSON.parse(accessInfo);
    //     }
    //     let access_token = accessInfo.access_token;
    //     var ret = yield userService.handleWxUserInfo(access_token,accessInfo.openid,ip,stoken,unionid);
    //     if(ret.code === 500){
    //         res.send(ret);
    //         return;
    //     }
    //     ret.token=yield tokenService.updateToken(ret.uid);
    //     ret.stoken=stoken;
    //     ret.phoneNoCheck=1;
    //     ret.version=version;
    //     ret.code=200;
    //     if(accessInfo.unionid) { ret.weixinUid=accessInfo.unionid; }
    //     xxCode=yield   userService.makeXxCode(ret.uid)
    //     ret.xxCode=xxCode;
    //     if(ret.phoneCode === ret.codeNocheck && ret.phoneCode)  ret.phoneNoCheck=0;
    //     console.logw('login ret',JSON.stringify(ret));
    //     res.send(ret);
    //     return;
    // });
    doAsync().catch(function(e) {
        console.logw('error', e.message);
        if(!res.finished)
            res.send({code:500, errmsg:"create user exception ."});
    });
});


function test(){

}


//微信公众号配置服务器的验证
router.get('/weixin/auth',function(req,res){
    console.logw('wexin/auth');
    let query=req.query;
    let signature=query.signature;
    let timestamp=query.timestamp;
    let nonce=query.nonce;
    let echostr=query.echostr;
    //1）将token、timestamp、nonce三个参数进行字典序排序
    //2）将三个参数字符串拼接成一个字符串进行sha1加密
    //3）开发者获得加密后的字符串可与signature对比，标识该请求来源于微信
    let token=cnst.wx_auth_token;
    let secret=cnst.wx_auth_secret;
    let ss=[token,timestamp,nonce];
    ss.sort(function(a,b) {if(a>b) return 1;return -1;});
    let s=ss[0]+ss[1]+ss[2];
    console.logw('info',token,timestamp,nonce,signature,echostr);
    let sres=Token.encodeSha1(s,secret);
    console.logw("sres",sres,signature);
    if(sres===signature) {
        console.logw("验证成功");
        return res.send(echostr);
    }
    console.logw("验证失败");
    res.send('failed');
});

router.get('/weixin/wxapp_login',function(req,res){
    var code = req.query.code;
    console.logg("wxapp_login ",code)
    if(!code)   {

        res.send({code:500,msg:"code is null"})
        return;
    }

    var doAsync= P.coroutine(function*()
    {
        var  appCode=code;
        var os='';
        let  ip= ipUtil.getIp(req,res);
        let  stoken=Token.aesEncodeDef(ip);

        if(code.indexOf("app_")<0)
        {  appCode="app_"+code;
        }

        let  accessInfo=yield wxService.getWxCodeCache(appCode);
        if(!accessInfo)
        {
            var  appid=config.mm_appid;
            var  secret=config.mm_secret;
            accessInfo=yield wxUtil.getAccessTokenFull(appid,secret,code,os);
            if(accessInfo.errcode>0)
            {
                console.logg("ret ",{code:501,msg:"code已过期请重新拉取"})
                res.send({code:501,msg:"code已过期请重新拉取"})
                return;
            }

        }
        if(typeof accessInfo === 'string')
            accessInfo=JSON.parse(accessInfo);
        var  data=accessInfo;
        let time=(new Date().getTime()-accessInfo.tick)/1000;
        var access_token = data.access_token;
        var openid = data.openid;
        var  unionid=data.unionid;
        var   wxopenid="wx_"+openid;
        if(unionid) {
            yield  redis.setHashItem(redisConst.wxUnionMap,unionid,wxopenid);
            yield  redis.setHashItem(redisConst.wxUnionMap,wxopenid,unionid);
        }

        //判断access_token调用时间是否超时
        if(time >= accessInfo.expires_in){
            accessInfo = yield wxUtil.getAccessToken(code,os);
            if(typeof accessInfo === 'string')
                accessInfo=JSON.parse(accessInfo);
        }
        var access_token = accessInfo.access_token;
        var ret = yield userService.handleWxUserInfo(access_token,accessInfo.openid,ip,stoken,unionid);
        if(ret.code === 500){
            res.send(ret);
            return;
        }
        ret.token=yield tokenService.updateToken(ret.uid);
        ret.stoken=stoken;
        ret.phoneNoCheck=1;
        ret.code=200;
        if(accessInfo.unionid) { ret.weixinUid=accessInfo.unionid; }
        xxCode=yield   userService.makeXxCode(ret.uid)
        ret.xxCode=xxCode;
        if(ret.phoneCode === ret.codeNocheck && ret.phoneCode)  ret.phoneNoCheck=0;
        console.logw('login ret',JSON.stringify(ret));
        res.send(ret);

    })

    doAsync().catch(function(e) {
    console.logw('error', e.message);
    if(!res.finished)
        res.send({code:500, errmsg:"wx app create user exception ."});
    })
});

let onTimer = function () {
    //console.logw('test');
    let doAsync = P.coroutine(function* () {
        let len = console.logList.length;
        for (let i = 0; i < len; i++) {
            let s = console.logList.shift();
            if (s) {
                yield fileUtil.writePer15mAsync(s);
            }
        }
    });
    doAsync().catch(function (e) {
        console.logw('wirte File exception', e.message);
    });
};

setInterval(onTimer, 200);
//process.on('uncaughtException', noop)

